package com.ruoyi.project.module.shao.baseinfo.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.project.domain.YhAppointment;
import com.ruoyi.project.service.IYhAppointmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.List;

import static com.ruoyi.common.security.utils.SecurityUtils.getUsername;

/**
 * 预约信息Controller
 * 
 * @author ruoyi
 * @date 2023-03-09
 */
@RestController
@RequestMapping("/shao/appointment")
public class ShaoAppointmentController extends BaseController
{
    @Autowired
    private IYhAppointmentService yhAppointmentService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询预约信息列表
     */
//    @RequiresPermissions("shao:appointment:list")
    @GetMapping("/list")
    public TableDataInfo list(YhAppointment yhAppointment)
    {
        String username = getUsername();
           yhAppointment.setStudentId(username);
        startPage();
        List<YhAppointment> list = yhAppointmentService.selectYhAppointmentList(yhAppointment);
        return getDataTable(list);


    }

    /**
     * 导出预约信息列表
     */
//    @RequiresPermissions("shao:appointment:export")
    @Log(title = "预约信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YhAppointment yhAppointment)
    {
        List<YhAppointment> list = yhAppointmentService.selectYhAppointmentList(yhAppointment);
        ExcelUtil<YhAppointment> util = new ExcelUtil<YhAppointment>(YhAppointment.class);
        util.exportExcel(response, list, "预约信息数据");
    }

    /**
     * 获取预约信息详细信息
     */
//    @RequiresPermissions("shao:appointment:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(yhAppointmentService.selectYhAppointmentById(id));
    }

    /**
     * 新增预约信息
     */
//    @RequiresPermissions("shao:appointment:add")
    @Log(title = "预约信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YhAppointment yhAppointment)
    {
        return toAjax(yhAppointmentService.insertYhAppointment(yhAppointment));
    }

    /**
     * 修改预约信息
     */
//    @RequiresPermissions("shao:appointment:edit")
    @Log(title = "预约信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YhAppointment yhAppointment)
    {
        /*ZoneId zoneId = ZoneId.systemDefault();
        Instant instant = yhAppointment.getAppointmentStart().toInstant();
        Instant instant1 = yhAppointment.getAppointmentEnd().toInstant();
        LocalDateTime localDateTime1 = instant.atZone(zoneId).toLocalDateTime();
        LocalDateTime localDateTime2 = instant1.atZone(zoneId).toLocalDateTime();
        long startTimestamp = localDateTime1.toEpochSecond(ZoneOffset.UTC);
        long endTimestamp = localDateTime2.toEpochSecond(ZoneOffset.UTC);
        String appointment = null + ":" + startTimestamp + ":" + endTimestamp;
        redisTemplate.opsForZSet().remove("appointments" + yhAppointment.getInstrumentId(), appointment);*/
        return toAjax(yhAppointmentService.updateYhAppointment(yhAppointment));
    }

    /**
     * 删除预约信息
     */
//    @RequiresPermissions("shao:appointment:remove")
    @Log(title = "预约信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        System.out.println("11111");
        return toAjax(yhAppointmentService.deleteYhAppointmentByIds(ids));
    }
}
